package com.caigou.modules.center.repository

import com.caigou.comm.base.bean.Banner
import com.caigou.comm.base.bean.HttpResult
import com.caigou.comm.base.bean.UserInfoBean
import com.caigou.modules.center.bean.*
import com.caigou.modules.center.network.HttpCenterService
import dagger.hilt.android.scopes.ActivityRetainedScoped
import kotlinx.coroutines.flow.Flow
import okhttp3.RequestBody.Companion.toRequestBody
import org.json.JSONArray
import org.json.JSONObject
import javax.inject.Inject

/**
 * @author #Suyghur.
 * Created on 2021/09/18
 */
@ActivityRetainedScoped
class CenterRepository @Inject constructor() {

    fun getBanners(): Flow<HttpResult<MutableList<Banner>>> {
        return HttpCenterService.service.getBanners()
    }

    fun getCsQQ(): Flow<HttpResult<CsQQBean>> {
        return HttpCenterService.service.getCsQQ()
    }

    fun getUserInfo(userId: String): Flow<HttpResult<UserInfoBean>> {
        return HttpCenterService.service.getUserInfo(userId)
    }

    fun getMyFollowList(page: Long, limit: Long, userId: String): Flow<HttpResult<MyFollowBean>> {
        return HttpCenterService.service.getMyFollowList(page, limit, userId)
    }

    fun getMyFansList(page: Long, limit: Long, userId: String): Flow<HttpResult<MyFansBean>> {
        return HttpCenterService.service.getMyFansList(page, limit, userId)
    }

    fun removeFans(fansUserIds: MutableList<String>, masterUserId: String): Flow<HttpResult<String>> {
        val jsonObject = JSONObject()
        kotlin.runCatching {
            val jsonArray = JSONArray()
            for (id in fansUserIds) {
                jsonArray.put(id)
            }
            jsonObject.put("fansUserIds", jsonArray)
            jsonObject.put("masterUserId", masterUserId)
        }
        val body = jsonObject.toString().toRequestBody()
        return HttpCenterService.service.removeFans(body)
    }

    fun getFlowerList(page: Long, limit: Long, userId: String): Flow<HttpResult<FlowerBean>> {
        return HttpCenterService.service.getFlowerList(page, limit, userId)
    }

    fun getRenameCardList(page: Long, limit: Long, userId: String): Flow<HttpResult<RenameCardBean>> {
        return HttpCenterService.service.getRenameCardList(page, limit, userId)
    }

    fun getDeleteCardList(page: Long, limit: Long, userId: String): Flow<HttpResult<DeleteCardBean>> {
        return HttpCenterService.service.getDeleteCardList(page, limit, userId)
    }

    fun getFreeCardList(page: Long, limit: Long, userId: String): Flow<HttpResult<FreeCardBean>> {
        return HttpCenterService.service.getFreeCardList(page, limit, userId)
    }

    fun getMyScoreDetail(userId: String): Flow<HttpResult<MyScoreDetailBean>> {
        return HttpCenterService.service.getMyScoreDetail(userId)
    }

    fun getScoreDetailList(page: Long, limit: Long, userId: String): Flow<HttpResult<ScoreDetailBean>> {
        return HttpCenterService.service.getScoreDetailList(page, limit, userId)
    }

    fun bugVip(amount: Int, userId: String): Flow<HttpResult<String>> {
        val jsonObject = JSONObject()
        jsonObject.put("amount", amount)
        jsonObject.put("userId", userId)
        val body = jsonObject.toString().toRequestBody()
        return HttpCenterService.service.bugVip(body)
    }

    fun getMessageList(): Flow<HttpResult<MutableList<MessageItemBean>>> {
        return HttpCenterService.service.getMessageList()
    }

    fun getGmList(userId: String): Flow<HttpResult<MutableList<MessageItemBean>>> {
        return HttpCenterService.service.getGmList(userId)
    }

    fun sendGmMsg(userId: String, content: String): Flow<HttpResult<String>> {
        val jsonObject = JSONObject()
        jsonObject.put("userId", userId)
        jsonObject.put("content", content)
        val body = jsonObject.toString().toRequestBody()
        return HttpCenterService.service.sendGmMsg(body)
    }

    fun getForumDynamicList(feedType: Int, userId: String, contentType: Int): Flow<HttpResult<DynamicBean>> {
        return HttpCenterService.service.getForumDynamicList(feedType, userId, contentType)
    }

    fun getInfoDynamicList(feedType: Int, userId: String): Flow<HttpResult<DynamicBean>> {
        return HttpCenterService.service.getInfoDynamicList(feedType, userId)
    }

}